import {useRouter} from "vue-router";

const $router = useRouter();

// 所有菜单数据
const menuDatas = [
  {
    id: "1",
    title: "工作台",
    path: "/Admin/WorkDesk",
    icon:"icon-menu"
  },
  {
    id: "2",
    title: "首页",
    path: "/Admin/Home",
  },

  {
    id: "3",
    title: "轮播图",
    children: [
      {
        id: "3-1",
        title: "轮播图列表",
        path: "/Admin/BannerList",
      },
      {
        id: "3-2",
        title: "添加轮播图",
        path: "/Admin/BannerAdd",
      },
    ],
  },
  {
    id: "4",
    title: "操作人员管理",
    children: [
      {
        id: "4-1",
        title: "操作人员列表",
        path: "/Admin/OperList",
      },
      {
        id: "4-2",
        title: "操作人员添加",
        path: "/Admin/AddOper",
      },
    ],
  },
  {
    id: "5",
    title: "商品管理",
    children: [
      {
        id: "5-1",
        title: "所有商品",
        path: "/Admin/SearchGoods",
      },
      {
        id: "5-2",
        title: "推荐商品",
        path: "/Admin/RecommendGoods",
      },
      {
        id: "5-3",
        title: "秒杀商品",
        path: "/Admin/SecondsKillGoods",
      },
    ],
  },
  {
    id: "6",
    title: "echarts示例",
    path:"/Admin/EchartsDemo"
  },
  {
    id: "7",
    title: "富文本示例",
    path:"/Admin/RichTextDemo"
  },
  {
    id: "8",
    title: "地图",
    path:"/Admin/MapDemo"
  }
];


// 根据当前用户的权限过滤出对应的菜单数据：

export const getCurrentMenu=()=>{
  // 1、获取当前用户的权限数据
  if(sessionStorage.getItem("checkedkeys")==null || sessionStorage.getItem("checkedkeys")==undefined){
    alert("亲，请求登录！");
    $router.push("/Login");
    return;
  }

  if(sessionStorage.getItem("checkedkeys")=="[]"){
    return menuDatas;
  }

  const checkedkeys = JSON.parse(sessionStorage.getItem("checkedkeys") as string);


  // 2、根据当前用户的权限过滤出对应的菜单数据： ["2","3","3-2"]

  let currMenuData = [];

  menuDatas.forEach((item)=>{
    if(checkedkeys.includes(item.id)){
      currMenuData.push({...item});

      if(item.children && Array.isArray(item.children) && item.children.length>0){
        
        currMenuData[currMenuData.length-1].children = [];

        item.children.forEach(sonItem=>{
          if(checkedkeys.includes(sonItem.id)){
            currMenuData[currMenuData.length-1].children.push({...sonItem});
          }
        })
      }
    }

  })

  return currMenuData;
}


